package com.mbengue.corp.apr.ra.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.mbengue.corp.apr.ra.core.Evenement;
import com.mbengue.corp.apr.ra.core.LieuEvenement;
import com.mbengue.corp.apr.ra.dao.LieuEvenementDao;
import com.mbengue.corp.apr.ra.util.HibernateUtil;

public class LieuEvenementDaoImpl implements LieuEvenementDao {
  private static final long serialVersionUID = 1L;
  Session session;

  public LieuEvenementDaoImpl() {
    session = HibernateUtil.getSessionFactory();
  }

  @Override
  public void ajouter(LieuEvenement aObjet) throws HibernateException {
    Transaction transaction = session.beginTransaction();
    try {
      session.save(aObjet);
      transaction.commit();
    } catch (Exception e) {
      transaction.rollback();
      throw new HibernateException(e);
    } finally {
      session.flush();
      //session.close();
    }
  }

  @Override
  public LieuEvenement modifier(LieuEvenement aObjet) throws HibernateException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void supprimer(LieuEvenement aObjet) throws HibernateException {
    Transaction tra = session.beginTransaction();
    session.delete(aObjet);
    tra.commit();
  }

  @Override
  public LieuEvenement trouver(Long aKey) throws HibernateException {
    Criteria criteria = session.createCriteria(LieuEvenement.class);
    criteria.add(Restrictions.eq("id", aKey));
    return (LieuEvenement) criteria.uniqueResult();
  }

  @Override
  public LieuEvenement trouverLieuEvenement(Evenement aEvenement) {
    Query query = session.getNamedQuery("findByEvenement");
    query.setParameter("evenement", aEvenement);
    return (LieuEvenement) query.uniqueResult();
  }
}
